<template>
  <div>
    <!-- 定义面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>医生列表</el-breadcrumb-item>
      <el-breadcrumb-item>医生信息</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 定义卡片 -->
    <el-card class="box-card">

      <!-- 定义第一行数据 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryItemInfo.query" clearable @clear="getDoctorList">
            <el-button slot="append" icon="el-icon-search" @click="getDoctorList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="toAddItem">新增医生</el-button>
        </el-col>
      </el-row>

      <!-- 获取商品列表数据 -->
      <el-table :data="doctorList" style="width: 100%" stripe border>
        <el-table-column type="index" label="序号">
        </el-table-column>
        <el-table-column prop="name" label="姓名">
        </el-table-column>
        <el-table-column prop="age" label="年龄" >
        </el-table-column>
        <!-- 使用过滤器修改时间 在main.js中定义过滤器 -->
        <el-table-column prop="sex" label="性别" >
        </el-table-column>
        <el-table-column prop="department" label="科室">
        </el-table-column>
        <el-table-column prop="bygood" label="主治症状" width="500px">
        </el-table-column>
       <!-- <el-table-column prop="updated" label="更新时间" width="180px" :formatter="formatDate">
        </el-table-column> -->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="updateItemBtn(scope.row)">修改</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteItemBtn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 定义分页插件-->

      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryItemInfo.pageNum" :page-sizes="[1, 5, 10, 20]" :page-size="queryItemInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>

    </el-card>

<el-dialog title="修改医生信息" :visible.sync="updateItemVisible" width="50%" >
      <span slot="footer" class="dialog-footer">
        <el-form :model="updateDoctorModel" :rules="rules" ref="updateUserRef" label-width="100px">
          <el-form-item label="序列" prop="id">
            <el-input v-model="updateDoctorModel.id"  disabled ></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="updateDoctorModel.name"></el-input>
          </el-form-item>
          <el-form-item label="年龄" prop="age">
            <el-input v-model="updateDoctorModel.age"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="sex" style="text-align: left;">
              <el-radio-group v-model="updateDoctorModel.sex">
                <el-radio label="男"></el-radio>
                <el-radio label="女"></el-radio>
              </el-radio-group>
          </el-form-item>
          <el-form-item label="科室" prop="sex" style="text-align: left;">
            <!-- 通过级联获取商品分类信息-->
            <el-cascader v-model="deparments" :options="hospitalList" :props="props"  @change="changeCascader">
            </el-cascader>
          </el-form-item>
          <el-form-item label="主治症状" prop="bygood">
            <el-input v-model="updateDoctorModel.bygood"></el-input>
          </el-form-item>
        </el-form>
        <el-button @click="updateItemVisible = false" >取 消</el-button>
        <el-button type="primary" @click="updateDoctor()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
updateItemVisible: false,
updateDoctorModel: {},
        //1.定义商品列表信息
        doctorList: [],
        //2.定义分页对象
        queryItemInfo: {
          query: '', //定义查询参数
          pageNum: 1,
          pageSize: 10
        },
        total: 0,
        rules: {
          name: [{
            required: true,
            message: '请输入医生姓名',
            trigger: 'blur'
          }],
          age: [{
            required: true,
            message: '请输入医生年龄',
            trigger: 'blur'
          }],
          sex: [{
            required: true,
            message: '请输入医生性别',
            trigger: 'blur'
          }],
          bygood: [{
            required: true,
            message: '请输入主治症状',
            trigger: 'blur'
          }],
        },
        deparments: [],
        hospitalList: [],
        props: {
          expandTrigger: 'hover',
          value: "name", //选中数据的value值
          label: "name", //选中数据展现名称
          children: "children", //自选项数据
        },
      }
    },
    created() {
      //1.获取商品列表数据
      this.getDoctorList()
      this.findhospitalList()
    },
    methods: {
      async findhospitalList() {
         const {
           data: result } = await this.$http.get("/hospital/findHospitalList/2")
         this.hospitalList = result.data
       },
       //修改商品分类选项
      async changeCascader() {
        /* const {
           data: result } =  await this.$http.get("/hospital/findHospitalById",{
             params: {
               id: this.deparments[1]
             }
           })
         this.deparments[2] = result.data */
         this.updateDoctorModel.department=this.deparments[1]
         console.log(this.deparments)
       },
      //实现商品信息分页查询
      async getDoctorList() {
        const {
          data: result
        } = await this.$http.get("/doctor/getDoctorList", {
          params: this.queryItemInfo
        })
        if (result.status !== 200) return this.$message.error("商品列表查询失败")
        this.doctorList = result.data.rows
        this.total = result.data.total
      },
      //条数变化时 调用
      handleSizeChange(size) {
        this.queryItemInfo.pageSize = size
        this.getDoctorList()
      },
      //页码值变化时 调用
      handleCurrentChange(current) {
        this.queryItemInfo.pageNum = current
        this.getDoctorList()
      },
      async deleteItemBtn(item) {
        //消息确认框
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          //根据id删除数据
          const {
            data: result
          } = await this.$http.delete("/doctor/deleteDoctorById", {
            params: {
              id: item.id
            }
          })
          if (result.status !== 200) return this.$message.error("商品删除失败")
          this.$message.success("商品删除成功")
          //重新获取商品列表信息
          this.getDoctorList()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      //转向到商品新增页面
      toAddItem(){
        this.$router.push("/doctor/addDoctor")
      },
      async updateItemBtn(item){
        this.updateItemVisible = true;
        const {data: result} = await this.$http.get("/doctor/"+item.id)
        if(result.status !== 200) return this.$message.error("用户查询失败")
        this.updateDoctorModel = result.data
        this.deparments = result.data.department
         console.log(this.deparments)
      },
      updateDoctor(){
        //1.预校验数据
        this.$refs.updateUserRef.validate(async valid => {
           if(!valid)  return this.$message.error("表单验证没有通过")
           //根据接口文档要求封装数据
           let doctor = {}
           doctor.id = this.updateDoctorModel.id
           doctor.name = this.updateDoctorModel.name
           doctor.age = this.updateDoctorModel.age
           doctor.sex = this.updateDoctorModel.sex
           doctor.department = this.updateDoctorModel.department
           doctor.bygood = this.updateDoctorModel.bygood
           const {data: result} = await this.$http.put(`/doctor/updateDoctor`,doctor)
           this.$message.success("用户更新成功")
           this.updateItemVisible = false
           this.getDoctorList()
        })
      }
    }
  }
</script>
<!-- 防止组件样式冲突 -->
<style lang="less" scoped>

</style>
